package org.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.config.R;
import org.example.domain.Crop;
import org.example.service.CropService;
import org.springframework.beans.factory.annotation.Autowired;
import org.example.domain.*;
import org.example.feign.UserDeptFeign;
import org.example.feign.UserFeign;
import org.example.service.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("crop")
public class CropController {
    @Autowired
    CropService cropService;
    @Autowired
    PeaService peaService;
    @RequestMapping("cropList")
    public R cropList(Page page){
        PageHelper.startPage((int) page.getCurrent(), (int) page.getSize());
        List<Crop> list = cropService.cropList();
        PageInfo<Crop> cropPageInfo = new PageInfo<>(list);
        return R.successed(cropPageInfo);
    }

    @RequestMapping("peaList")
    public R peaList(String cropNo){
        Pea pea = peaService.getByCropNo(cropNo);
        return R.successed(pea);
    }

    @Autowired
    UserFeign userFeign;

    @Autowired
    UserDeptFeign userDeptFeign;

    @RequestMapping("cList")
    public R cList(){
        List<User> list = userFeign.getUserName();
        return R.successed(list);
    }

    @RequestMapping("dList")
    public R dList(){
        List<UserDept> list = userDeptFeign.getDeptName();
        return R.successed(list);
    }

    @Autowired
    CarService carService;
    @Autowired
    MaterialService materialService;
    @Autowired
    AddressService addressService;
    @RequestMapping("addCar")
    public R addCar(@RequestBody @Valid Car car, BindingResult result){
        if (result.hasErrors()){
            String defaultMessage = result.getAllErrors().get(0).getDefaultMessage();
            return R.failed(defaultMessage);
        }
        car.setCreateTime(new Date());
        car.setStatus(1);
        carService.addCar(car);
        Material material = new Material();
        material.setStatus(0);
        material.setGoodsNo(car.getGoodsNo());
        material.setSendName(car.getSendName());
        material.setGoodsFrom(car.getGoodsFrom());
        material.setCreateTime(new Date());
        materialService.materialSave(material);

        cropService.upByGoodsNO(car.getGoodsNo());
        return R.successed(null);
    }

    @RequestMapping("materialList")
    public R materialList(){
        List<Material> list = materialService.materialList();
        return R.successed(list);
    }

    @RequestMapping("updateById")
    public R updateById(Integer id){
        return materialService.updateById(id);
    }

    @RequestMapping("updateByIdStatus")
    public R updateByIdStatus(Integer id){
        return materialService.updateByIdStatus(id);
    }

}
